package com.cq.hd.member.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cq.hd.common.constant.Constant;
import com.cq.hd.common.entity.hf.HfBankPay;
import com.cq.hd.common.entity.hf.HfTradeQuery;
import com.cq.hd.common.enums.biz.*;
import com.cq.hd.common.pagination.Page;
import com.cq.hd.common.response.Throw;
import com.cq.hd.common.utils.GenerateUtil;
import com.cq.hd.common.utils.LocalDateUtil;
import com.cq.hd.common.utils.NoUtils;
import com.cq.hd.common.utils.StringUtils;
import com.cq.hd.member.api.dto.AdminBankPayRecordPageDto;
import com.cq.hd.member.api.dto.BankPayQueryDto;
import com.cq.hd.member.api.dto.BankPayRechargeDto;
import com.cq.hd.member.api.vo.AdminBankPayRecordPageVo;
import com.cq.hd.member.api.vo.BankPayQueryVo;
import com.cq.hd.member.api.vo.BankPayRechargeVo;
import com.cq.hd.member.config.HDFinancialAccountProperties;
import com.cq.hd.member.mapper.TbBusinessMapper;
import com.cq.hd.member.mapper.TbSubsidyAmtBillMapper;
import com.cq.hd.member.po.TbBankPayRecordPo;
import com.cq.hd.member.mapper.TbBankPayRecordMapper;
import com.cq.hd.member.po.TbBusinessPo;
import com.cq.hd.member.po.TbSubsidyAmtBillPo;
import com.cq.hd.member.service.TbBankPayRecordService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cq.hd.system.api.HfApi;
import com.cq.hd.system.api.HfRequestRecordApi;
import com.cq.hd.system.api.dto.HfRequestRecordSaveDto;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.interceptor.DefaultTransactionAttribute;
import org.springframework.util.CollectionUtils;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 网银支付记录表 服务实现类
 * </p>
 *
 * @author mYunYu
 * @since 2024-02-21
 */
@Slf4j
@Service
public class TbBankPayRecordServiceImpl extends ServiceImpl<TbBankPayRecordMapper, TbBankPayRecordPo> implements TbBankPayRecordService {

    @Autowired
    private HfApi hfApi;

    @Autowired
    private TbBusinessMapper businessMapper;

    @Autowired
    private TbSubsidyAmtBillMapper subsidyAmtBillMapper;

    @Autowired
    private PlatformTransactionManager transactionManager;

    @Autowired
    private HfRequestRecordApi hfRequestRecordApi;

    @Autowired
    private HDFinancialAccountProperties hdFinancialAccountProperties;

    @Override
    public BankPayRechargeVo recharge(BankPayRechargeDto bankPayRechargeDto) {
        Long businessId = bankPayRechargeDto.getBusinessId();
        TbBusinessPo businessPo = businessMapper.selectById(businessId);

        String hfMerchantNo;
        if (FinancialAccountTypeEnum.PLATFORM.getValue() == businessPo.getFinancialAccountType()) {
            // 如果是结算池商家，补贴款进入平台结算池补贴收款账户
            hfMerchantNo = hdFinancialAccountProperties.getHfMerchantNo();
        } else {
            hfMerchantNo = businessPo.getHfMerchantNo();
            if (StringUtils.isBlank(hfMerchantNo)) {
                log.error("支付失败，未正式入驻成功的商家不能使用网银支付，businessId：{}", businessId);
                Throw.isBusinessException("未正式入驻成功的商家不能使用网银支付");
            }
        }

        BigDecimal rechargeAmt = bankPayRechargeDto.getRechargeAmt();
        // 交易金额=输入充值金额+网银手续费
        BigDecimal tradeAmt = rechargeAmt.add(Constant.BANK_PAY_AMT);

        LocalDateTime nowLocalDateTime = LocalDateUtil.getNowLocalDateTime();
        String yyyyMMdd = LocalDateUtil.toLocalDateToString(nowLocalDateTime.toLocalDate(), "yyyyMMdd");
        String reqSeqId = LocalDateUtil.toLocalDateTimeToString(nowLocalDateTime, "yyyyMMddHHmmss") + GenerateUtil.getRandomNum(5);

        LocalDateTime timeExpireLocalDateTime = nowLocalDateTime.plusMinutes(120);
        String timeExpire = LocalDateUtil.toLocalDateTimeToString(timeExpireLocalDateTime, "yyyyMMddHHmmss");

        HfBankPay hfBankPay = new HfBankPay();
        hfBankPay.setHuifuId(hfMerchantNo);
        hfBankPay.setReqSeqId(reqSeqId);
        hfBankPay.setReqDate(yyyyMMdd);
        hfBankPay.setTimeExpire(timeExpire);
        hfBankPay.setTransAmt(tradeAmt.toString());
        hfBankPay.setGoodsDesc("充值-补贴账号");
        hfBankPay.setRequestIp(bankPayRechargeDto.getRequestIp());
        hfBankPay.setRemark(String.valueOf(BankPayTradeTypeEnum.RECHARGE_SUBSIDY.getValue()));
        JSONObject jsonObject = hfApi.bankPay(hfBankPay).unpack();
        if (jsonObject == null) {
            log.error("支付失败，调用hf网银支付接口失败，businessId：{}", businessId);
            Throw.isBusinessException("支付失败，请联系客服");
        }

        JSONObject dataJSONObject = jsonObject.getJSONObject("data");
        String respCode = dataJSONObject.getString("resp_code");
        String respDesc = dataJSONObject.getString("resp_desc");
        if (!"00000000".equals(respCode)) {
            Throw.isBusinessException(respDesc);
        }

        String formUrl = dataJSONObject.getString("form_url");
        // P：处理中，S：成功，F：失败，I：初始化（停留在操作页面没选银行支付会是I状态）；
        String transStat = dataJSONObject.getString("trans_stat");

        int tradeState = BankPayTradeStateEnum.WAIT_PAY.getValue();
        if ("P".equals(transStat)) {
            tradeState = BankPayTradeStateEnum.PROCESSING.getValue();
        } else if ("S".equals(transStat)) {
            tradeState = BankPayTradeStateEnum.SUCCESS.getValue();
        } else if ("F".equals(transStat)) {
            tradeState = BankPayTradeStateEnum.FAIL.getValue();
        }

        TbBankPayRecordPo tbBankPayRecordPo = new TbBankPayRecordPo();
        tbBankPayRecordPo.setBusinessId(businessId);
        tbBankPayRecordPo.setUserId(bankPayRechargeDto.getUserId());
        tbBankPayRecordPo.setOrderNo(NoUtils.getBankPayOrderNo());
        tbBankPayRecordPo.setTradeType(BankPayTradeTypeEnum.RECHARGE_SUBSIDY.getValue());
        tbBankPayRecordPo.setHfId(hfMerchantNo);
        tbBankPayRecordPo.setReqSeqId(reqSeqId);
        tbBankPayRecordPo.setReqDate(yyyyMMdd);
        tbBankPayRecordPo.setTimeExpire(timeExpire);
        tbBankPayRecordPo.setRequestIp(bankPayRechargeDto.getRequestIp());
        tbBankPayRecordPo.setTradeAmt(tradeAmt);
        tbBankPayRecordPo.setFeeAmt(Constant.BANK_PAY_AMT);
        tbBankPayRecordPo.setReceiveAmt(rechargeAmt);
        tbBankPayRecordPo.setTradeState(tradeState);
        tbBankPayRecordPo.setAcctState(BankPayTradeStateEnum.WAIT_PAY.getValue());
        tbBankPayRecordPo.setTradeTime(LocalDateTime.now());
        tbBankPayRecordPo.setCreateTime(LocalDateTime.now());
        tbBankPayRecordPo.setUpdateTime(LocalDateTime.now());
        int insert = baseMapper.insert(tbBankPayRecordPo);
        if (insert == 0) {
            log.error("支付失败，添加网银支付记录失败，businessId：{}", businessId);
            Throw.isBusinessException("支付失败，请联系客服");
        }

        BankPayRechargeVo bankPayRechargeVo = new BankPayRechargeVo();
        bankPayRechargeVo.setFromUrl(formUrl);
        bankPayRechargeVo.setOrderNo(tbBankPayRecordPo.getOrderNo());

        return bankPayRechargeVo;
    }

    @Override
    public BankPayQueryVo queryOrderPay(BankPayQueryDto bankPayQueryDto) {
        String orderNo = bankPayQueryDto.getOrderNo();
        if (StringUtils.isBlank(orderNo)) {
            Throw.isBusinessException("订单号不能为空");
        }

        List<TbBankPayRecordPo> tbBankPayRecordPos = baseMapper.selectList(new QueryWrapper<TbBankPayRecordPo>().eq("del_state", 0)
                .eq("order_no", orderNo)
                .orderByDesc("create_time"));
        if (CollectionUtils.isEmpty(tbBankPayRecordPos)) {
            log.error("查单失败，未找到网银支付订单数据，orderNo：{}", orderNo);
            Throw.isBusinessException("未找到订单");
        }

        TbBankPayRecordPo tbBankPayRecordPo = tbBankPayRecordPos.get(0);
        // 交易状态(1-待支付，2-处理中，3-成功，4-失败)
        Integer tradeState = tbBankPayRecordPo.getTradeState();

        BankPayQueryVo bankPayQueryVo = new BankPayQueryVo();
        bankPayQueryVo.setTradeState(tradeState);

        if (BankPayTradeStateEnum.SUCCESS.getValue().equals(tradeState) || BankPayTradeStateEnum.FAIL.getValue().equals(tradeState)) {
            return bankPayQueryVo;
        }

        // 如果是待支付/处理中交易状态，需要调用hf接口做查单处理
        HfTradeQuery hfTradeQuery = new HfTradeQuery();
        hfTradeQuery.setHfMerchantNo(tbBankPayRecordPo.getHfId());
        hfTradeQuery.setOrgReqDate(tbBankPayRecordPo.getReqDate());
        hfTradeQuery.setOrgReqSeqId(tbBankPayRecordPo.getReqSeqId());
        JSONObject jsonObject = hfApi.bankPayQuery(hfTradeQuery).unpack();
        if (jsonObject != null) {
            JSONObject dataJSONObject = jsonObject.getJSONObject("data");
            // P：处理中，S：成功，F：失败，I：初始化（停留在操作页面没选银行支付会是I状态）；
            String transStat = dataJSONObject.getString("trans_stat");
            log.info("查网银支付订单成功，订单第三方交易状态：【{}】，orderNo：{}", transStat, orderNo);

            if ("S".equals(transStat)) {
                // 支付成功，更新网银支付记录状态，增加补贴金，增加补贴金流水数据
                tbBankPayRecordPo.setTradeState(BankPayTradeStateEnum.SUCCESS.getValue());
                tbBankPayRecordPo.setCallbackTime(LocalDateTime.now());
                tbBankPayRecordPo.setUpdateTime(LocalDateTime.now());
                // hf全局流水号
                tbBankPayRecordPo.setHfSeqId(dataJSONObject.getString("org_hf_seq_id"));

                JSONObject bankExtendParamData = dataJSONObject.getJSONObject("bank_extend_param_data");
                if (bankExtendParamData != null) {
                    // 付款方银行号
                    tbBankPayRecordPo.setBankId(bankExtendParamData.getString("bank_id"));
                    // 付款方银行账户
                    tbBankPayRecordPo.setPyerAcctId(bankExtendParamData.getString("pyer_acct_id"));
                    // 付款方银行账户名
                    tbBankPayRecordPo.setPyerAcctNm(bankExtendParamData.getString("pyer_acct_nm"));
                }

                paySuccess(tbBankPayRecordPo);
                bankPayQueryVo.setTradeState(BankPayTradeStateEnum.SUCCESS.getValue());
            } else if ("F".equals(transStat)) {
                // 支付失败，更新网银支付记录状态和失败原因
                String bankDesc = dataJSONObject.getString("bank_desc");

                tbBankPayRecordPo.setTradeState(BankPayTradeStateEnum.FAIL.getValue());
                tbBankPayRecordPo.setFailReason(bankDesc);
                tbBankPayRecordPo.setHfFailReason(bankDesc);
                tbBankPayRecordPo.setCallbackTime(LocalDateTime.now());
                tbBankPayRecordPo.setUpdateTime(LocalDateTime.now());
                baseMapper.updateById(tbBankPayRecordPo);

                bankPayQueryVo.setTradeState(BankPayTradeStateEnum.FAIL.getValue());
            } else if ("P".equals(transStat)) {
                // 处理中，更新网银支付记录状态
                tbBankPayRecordPo.setTradeState(BankPayTradeStateEnum.PROCESSING.getValue());
                tbBankPayRecordPo.setUpdateTime(LocalDateTime.now());
                baseMapper.updateById(tbBankPayRecordPo);

                bankPayQueryVo.setTradeState(BankPayTradeStateEnum.PROCESSING.getValue());
            }
        }

        return bankPayQueryVo;
    }

    private void paySuccess(TbBankPayRecordPo tbBankPayRecordPo) {
        // 如果是充值补贴金，更新网银支付记录状态，增加补贴金，增加补贴金流水数据（两条）
        Long businessId = tbBankPayRecordPo.getBusinessId();
        String orderNo = tbBankPayRecordPo.getOrderNo();
        BigDecimal tradeAmt = tbBankPayRecordPo.getTradeAmt();
        BigDecimal feeAmt = tbBankPayRecordPo.getFeeAmt();
        BigDecimal receiveAmt = tbBankPayRecordPo.getReceiveAmt();

        TbBusinessPo businessPo = businessMapper.selectById(businessId);
        BigDecimal subsidyAmt = businessPo.getSubsidyAmt();

        TransactionStatus transaction = transactionManager.getTransaction(new DefaultTransactionAttribute());

        try {
            // 更新网银支付记录状态
            int count = baseMapper.updateById(tbBankPayRecordPo);
            if (count == 0) {
                log.error("更新网银支付记录状态失败，tbBankPayRecordId：{}", tbBankPayRecordPo.getId());
                Throw.isBusinessException("更新网银支付记录失败");
            }

            // 给商家增加补贴金
            count = businessMapper.addSubsidyAmt(businessId, tbBankPayRecordPo.getReceiveAmt());
            if (count == 0) {
                log.error("给商家增加补贴金失败，tbBankPayRecordId：{}", tbBankPayRecordPo.getId());
                Throw.isBusinessException("给商家增加补贴金失败");
            }

            // 增加补贴金流水(两条，补贴充值一条，手续费一条)
            TbSubsidyAmtBillPo tbSubsidyAmtBillPo = new TbSubsidyAmtBillPo();
            tbSubsidyAmtBillPo.setBusinessId(businessId);
            tbSubsidyAmtBillPo.setUserId(tbBankPayRecordPo.getUserId());
            tbSubsidyAmtBillPo.setOrderNo(NoUtils.getSubsidyOrderNo());
            tbSubsidyAmtBillPo.setBillType(BillTypeEnum.INCOME.getValue());
            tbSubsidyAmtBillPo.setTradeType(SubsidyTradeTypeEnum.RECHARGE.getValue());
            tbSubsidyAmtBillPo.setRelationTradeNo(orderNo);
            tbSubsidyAmtBillPo.setBeforeAmt(subsidyAmt);
            tbSubsidyAmtBillPo.setTradeAmt(tradeAmt);
            tbSubsidyAmtBillPo.setAfterAmt(subsidyAmt.add(tradeAmt));
            tbSubsidyAmtBillPo.setPayState(PayStateEnum.SUCCESS.getValue());
            tbSubsidyAmtBillPo.setTradeTime(tbBankPayRecordPo.getCallbackTime());
            tbSubsidyAmtBillPo.setCreateTime(LocalDateTime.now());
            tbSubsidyAmtBillPo.setUpdateTime(LocalDateTime.now());
            count = subsidyAmtBillMapper.insert(tbSubsidyAmtBillPo);
            if (count == 0) {
                log.error("增加商家补贴金充值流水失败，tbBankPayRecordId：{}", tbBankPayRecordPo.getId());
                Throw.isBusinessException("增加商家补贴金充值流水失败");
            }

            tbSubsidyAmtBillPo = new TbSubsidyAmtBillPo();
            tbSubsidyAmtBillPo.setBusinessId(businessId);
            tbSubsidyAmtBillPo.setUserId(tbBankPayRecordPo.getUserId());
            tbSubsidyAmtBillPo.setOrderNo(NoUtils.getSubsidyOrderNo());
            tbSubsidyAmtBillPo.setBillType(BillTypeEnum.EXPEND.getValue());
            tbSubsidyAmtBillPo.setTradeType(SubsidyTradeTypeEnum.FEE.getValue());
            tbSubsidyAmtBillPo.setRelationTradeNo(orderNo);
            tbSubsidyAmtBillPo.setBeforeAmt(subsidyAmt.add(tradeAmt));
            tbSubsidyAmtBillPo.setTradeAmt(feeAmt);
            tbSubsidyAmtBillPo.setAfterAmt(subsidyAmt.add(receiveAmt));
            tbSubsidyAmtBillPo.setPayState(PayStateEnum.SUCCESS.getValue());
            tbSubsidyAmtBillPo.setTradeTime(tbBankPayRecordPo.getCallbackTime());
            tbSubsidyAmtBillPo.setCreateTime(LocalDateTime.now());
            tbSubsidyAmtBillPo.setUpdateTime(LocalDateTime.now());
            count = subsidyAmtBillMapper.insert(tbSubsidyAmtBillPo);
            if (count == 0) {
                log.error("增加商家补贴金手续费流水失败，tbBankPayRecordId：{}", tbBankPayRecordPo.getId());
                Throw.isBusinessException("增加商家补贴金手续费流水失败");
            }

            transactionManager.commit(transaction);
        } catch (Exception e) {
            //回滚事务
            transactionManager.rollback(transaction);
            log.error("<网银支付充值补贴金>事务异常回滚", e);
        }
    }

    @Override
    public Page<AdminBankPayRecordPageVo> pageAdminBankPayRecord(AdminBankPayRecordPageDto adminBankPayRecordPageDto) {
        String startTradeTime = adminBankPayRecordPageDto.getStartTradeTime();
        if (!StringUtils.isBlank(startTradeTime)) {
            try {
                LocalDateUtil.toLocalDateTime(startTradeTime, Constant.YYYY_MM_DD_HH_MM_SS);
            } catch (Exception e) {
                Throw.isBusinessException("开始提交时间格式错误");
            }
        }

        String endTradeTime = adminBankPayRecordPageDto.getEndTradeTime();
        if (!StringUtils.isBlank(endTradeTime)) {
            try {
                LocalDateUtil.toLocalDateTime(endTradeTime, Constant.YYYY_MM_DD_HH_MM_SS);
            } catch (Exception e) {
                Throw.isBusinessException("结束提交时间格式错误");
            }
        }

        PageHelper.startPage(adminBankPayRecordPageDto.getPageNum(), adminBankPayRecordPageDto.getPageSize());
        List<AdminBankPayRecordPageVo> adminBankPayRecordPageVos = baseMapper.listAdminBankPayRecord(adminBankPayRecordPageDto);
        PageInfo<AdminBankPayRecordPageVo> pageInfo = new PageInfo<>(adminBankPayRecordPageVos);
        return Page.newPageWithAllFields(pageInfo.getList(), pageInfo.getPageNum(), pageInfo.getPageSize(), pageInfo.getTotal(), pageInfo.getPages());
    }

    /**
     * 2条异步通知分别是 交易异步 跟 账务异步，用 notify_type 字段区分。
     * 1). 交易异步情况
     * notify_type='1'，trans_stat='F' 时，不推送账务异步
     * notify_type='1'，trans_stat='S' 时，会推送账务异步
     * 2). 账务异步情况：
     * notify_type='2'，trans_stat='S'，acct_stat='S' 表示交易成功-入账成功
     * notify_type='2'，trans_stat='S'，acct_stat='F' 表示交易成功-入账失败（非正常情况，可联系汇付技术人员确认排查）
     */
    @Override
    public Boolean bankPayCallback(JSONObject dataObj) {
        String subRespCode = dataObj.getString("sub_resp_code");
        String subRespDesc = dataObj.getString("sub_resp_desc");
        String hfMerchantNo = dataObj.getString("huifu_id");
        String reqDate = dataObj.getString("req_date");
        String reqSeqId = dataObj.getString("req_seq_id");
        //   // P：处理中，S：成功，F：失败，I：初始化（停留在操作页面没选银行支付会是I状态）；
        String transStat = dataObj.getString("trans_stat");
        //   // P：处理中，S：成功，F：失败，I：初始化（停留在操作页面没选银行支付会是I状态）；
        String acctStat = dataObj.getString("acct_stat");
        // 通知类型	1：通道通知，2：财务通知；
        String notifyType = dataObj.getString("notify_type");

        // 根据reqSeqId+reqDate查询网银支付记录
        List<TbBankPayRecordPo> tbBankPayRecordPos = baseMapper.selectList(new QueryWrapper<TbBankPayRecordPo>().eq("del_state", 0)
                .eq("trade_type", BankPayTradeTypeEnum.RECHARGE_SUBSIDY.getValue())
                .eq("req_date", reqDate)
                .eq("req_seq_id", reqSeqId).orderByDesc("create_time"));
        if (CollectionUtils.isEmpty(tbBankPayRecordPos)) {
            log.error("网银充值补贴金回调, 未找到网银支付记录数据, reqSeqId:{}, reqDate:{}", reqSeqId, reqDate);
            return true;
        }

        TbBankPayRecordPo tbBankPayRecordPo = tbBankPayRecordPos.get(0);

        // 添加汇付申请单记录（网银支付下单回调，异步回调接口）
        HfRequestRecordSaveDto hfRequestRecordSaveDto = new HfRequestRecordSaveDto();
        hfRequestRecordSaveDto.setApplyType(HfRequestRecordApplyTypeEnum.BANK_PAY_ASYNC.getValue());
        hfRequestRecordSaveDto.setRequestType(HfRequestRecordRequestTypeEnum.ASYNC.getValue());
        hfRequestRecordSaveDto.setApplyNo(PayAttachEnum.RECHARGE_SUBSIDY_PAY.getName());
        hfRequestRecordSaveDto.setHuifuId(hfMerchantNo);
        hfRequestRecordSaveDto.setHfApplyNo(tbBankPayRecordPo.getOrderNo());
        hfRequestRecordSaveDto.setDataResult(JSON.toJSONString(dataObj));
        hfRequestRecordSaveDto.setReqSeqId(reqSeqId);
        hfRequestRecordSaveDto.setRespCode(subRespCode);
        hfRequestRecordSaveDto.setRespDesc(subRespDesc);
        hfRequestRecordSaveDto.setCreateTime(LocalDateTime.now());
        hfRequestRecordSaveDto.setUpdateTime(LocalDateTime.now());
        Boolean saveRequestRecordState = hfRequestRecordApi.saveRequestRecord(hfRequestRecordSaveDto).unpack();
        if (!saveRequestRecordState) {
            log.error("添加汇付申请单记录失败（网银支付下单回调-充值补贴金，异步回调接口），hfRequestRecordSaveDto：{}", JSON.toJSONString(hfRequestRecordSaveDto));
        }

        if (BankPayTradeStateEnum.FAIL.getValue().equals(tbBankPayRecordPo.getTradeState())
                || BankPayTradeStateEnum.SUCCESS.getValue().equals(tbBankPayRecordPo.getTradeState())) {
            log.error("网银充值补贴金回调, 订单已处理, reqSeqId:{}, reqDate:{}", reqSeqId, reqDate);
            return true;
        }

        if ("00000000".equals(subRespCode)) {
            if ("S".equals(transStat)) {
                // 交易成功
                if ("S".equals(acctStat)) {
                    // 入账成功
                    paySuccess(tbBankPayRecordPo);
                } else {
                    // 只更新财务状态,但不更新交易状态,也不增加资金
                    if ("P".equals(acctStat)) {
                        baseMapper.updateAcctState(tbBankPayRecordPo.getId(), BankPayTradeStateEnum.PROCESSING.getValue());
                    } else if ("F".equals(acctStat)) {
                        baseMapper.updateAcctState(tbBankPayRecordPo.getId(), BankPayTradeStateEnum.FAIL.getValue());
                    }
                }
            } else if ("F".equals(transStat)) {
                tbBankPayRecordPo.setTradeState(BankPayTradeStateEnum.FAIL.getValue());
                tbBankPayRecordPo.setFailReason(dataObj.getString("bank_desc"));
                tbBankPayRecordPo.setHfFailReason(JSON.toJSONString(dataObj));
                baseMapper.updateById(tbBankPayRecordPo);
            }
        } else {
            if ("F".equals(transStat)) {
                tbBankPayRecordPo.setTradeState(BankPayTradeStateEnum.FAIL.getValue());
                tbBankPayRecordPo.setFailReason(dataObj.getString("bank_desc"));
                tbBankPayRecordPo.setHfFailReason(JSON.toJSONString(dataObj));
                baseMapper.updateById(tbBankPayRecordPo);
            }
        }

        return true;
    }
}
